package com.javabase.y2022.m04.day0423.v0069.v2;

/**
 * @descriptions:
 * @author: Tom 使用二分法
 * @date: 2022/4/23 上午 08:49
 * @version: 1.0
 */
public class Solution {
    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.mySqrt(2147395600));
    }
    public int mySqrt(int x){
        if( x == 0){
            return 0;
        }
        int ans = 1;
        int left = 1;
        int right = x/2;
        while(left <= right){
            int mid = left + ((right - left) >> 1);
            long midMulti = (long)mid * mid;
            if(midMulti == x){
                return mid;
            }else if(midMulti > x){
                right = mid - 1;
            } else {
                ans = mid;
                left = mid + 1;
            }
        }

        return ans;
    }
}
